package cn.xspace.wnx.db;

import java.util.ArrayList;

import cn.xspace.wnx.data.INoticeWnxData;
import cn.xspace.wnx.entity.WNXBean;
import rx.Observable;
import rx.Subscriber;

/**
 * 通知公告和微农信的Dao
 * Created by liuchen on 2016/11/12.
 */
public class NoticeDao {
    private static NoticeDao instance;
    public static final String TABLE_NAME = "t_wnxlist";
    public static final String COLUMN_TIME_STAMP = "inserttime";
    /**
     * 0：通知公告；1：微农信；
     */
    public static final String COLUMN_MSG_ID = "msg_id";
    public static final String COLUMN_MSG_TYPE = "msg_type";

    public static final String COLUMN_MSG_LOGO = "msg_logo";
    public static final String COLUMN_MSG_SENDER_ID = "msg_sender_id";
    public static final String COLUMN_MSG_SENDER_NAME = "msg_sender_name";
    public static final String COLUMN_MSG_UNREAD_FLAG = "msg_unread_flag";
    public static final String COLUMN_MSG_SEND_TIME = "msg_send_time";
    public static final String COLUMN_MSG_CONTENT_TYPE = "msg_content_type";
    //工作通讯 农业新闻 技术指导 农业预警 市场咨询 政策解读 致富快报
    public static final String COLUMN_MSG_KIND = "msg_kind";
    public static final String COLUMN_MSG_CONTENT = "content";
    public static final String COLUMN_MSG_TITLE = "msg_title";//第一条title
    public static final String COLUMN_REMARK1 = "remark1";
    public static final String COLUMN_REMARK2 = "remark2";
    public static final String COLUMN_REMARK3 = "remark3"; //作为wnxBean类型区分使用

    public static NoticeDao getInstance() {
        if (instance == null) {
            instance = new NoticeDao();
        }
        return instance;
    }

    private NoticeDao() {
    }

    public static final String WNX_LIST_TABLE_CREATE = "CREATE TABLE  IF NOT EXISTS "
            + TABLE_NAME
            + " (_ID INTEGER PRIMARY KEY AUTOINCREMENT,"
            + COLUMN_TIME_STAMP + " TEXT,"
            + COLUMN_MSG_ID + " TEXT,"
            + COLUMN_MSG_TYPE + " INTEGER,"
            + COLUMN_MSG_LOGO + " TEXT,"
            + COLUMN_MSG_SENDER_ID + " TEXT,"
            + COLUMN_MSG_SENDER_NAME + " TEXT,"
            + COLUMN_MSG_UNREAD_FLAG + " INTEGER,"
            + COLUMN_MSG_SEND_TIME + " TEXT,"
            + COLUMN_MSG_CONTENT_TYPE + " INTEGER,"
            + COLUMN_MSG_KIND + " INTEGER,"
            + COLUMN_MSG_CONTENT + " TEXT,"
            + COLUMN_MSG_TITLE + " TEXT,"
            + COLUMN_REMARK1 + " TEXT,"
            + COLUMN_REMARK2 + " TEXT,"
            + COLUMN_REMARK3 + " INTEGER)";

    INoticeWnxData data = new INoticeWnxData() {
        @Override
        public Observable<String> insertWnxNotice(final WNXBean wnxBean) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().insertWnxNotice(wnxBean);
                    subscriber.onNext("success wnx");
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<String> deleteWnxNotice(final WNXBean wnxBean) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().deleleWnxNotice(wnxBean);
                    subscriber.onNext("success wnx");
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<String> deleteAllWnxNoticeByDepId(final String depId) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().deleleAllWnxNotice(depId);
                    subscriber.onNext("success wnx");
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<ArrayList<WNXBean>> getNoticeList() {
            return Observable.create(new Observable.OnSubscribe<ArrayList<WNXBean>>() {
                @Override
                public void call(Subscriber<? super ArrayList<WNXBean>> subscriber) {
                    subscriber.onNext(WNXDBManager.getInstance().getNoticeList());
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<ArrayList<WNXBean>> getWnxList() {
            return Observable.create(new Observable.OnSubscribe<ArrayList<WNXBean>>() {
                @Override
                public void call(Subscriber<? super ArrayList<WNXBean>> subscriber) {
                    subscriber.onNext(WNXDBManager.getInstance().getWnxList());
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<String> updateWnxOrgReadStatus(final String senderId) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().updateWnxOrgReadStatus(senderId);
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<String> updateWnxRecordReadStatus(final String inserttime, final int msg_type) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().updateNoticeRecordReadStatus(inserttime, msg_type);
                    subscriber.onNext("success");
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<String> updateNoticeReadStatus(final String inserttime) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().updateNoticceReadStatus(inserttime);
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<Integer> getNoticeCount() {
            return Observable.create(new Observable.OnSubscribe<Integer>() {
                @Override
                public void call(Subscriber<? super Integer> subscriber) {
                    subscriber.onNext(WNXDBManager.getInstance().getNoticeUnreadCount());
                    subscriber.onCompleted();

                }
            });
        }

        @Override
        public Observable<Integer> getGroupCount() {
            return Observable.create(new Observable.OnSubscribe<Integer>() {
                @Override
                public void call(Subscriber<? super Integer> subscriber) {
                    subscriber.onNext(WNXDBManager.getInstance().getGroupUnreadCount());
                    subscriber.onCompleted();

                }
            });
        }

        @Override
        public Observable<ArrayList<WNXBean>> getWnxListBySenderid(final String senderId, final int pageindex) {
            return Observable.create(new Observable.OnSubscribe<ArrayList<WNXBean>>() {
                @Override
                public void call(Subscriber<? super ArrayList<WNXBean>> subscriber) {
                    subscriber.onNext(WNXDBManager.getInstance().getWnxListBySenderId(senderId, pageindex));
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<Boolean> isNoticeWnxExist(final String msgId) {
            return Observable.create(new Observable.OnSubscribe<Boolean>() {
                @Override
                public void call(Subscriber<? super Boolean> subscriber) {
                    subscriber.onNext(WNXDBManager.getInstance().isNoticeWnxExist(msgId));
                }
            });
        }


    };

    public INoticeWnxData getData() {
        return data;
    }

    public int getNoticeCount() {
        return WNXDBManager.getInstance().getNoticeUnreadCount();
    }

    public int getWNXUnreadStatue() {
        return WNXDBManager.getInstance().getWNXUnreadStatue();
    }

    public int getGroupCount() {
        return WNXDBManager.getInstance().getGroupUnreadCount();
    }

    public Observable<String> saveWNXOrgList(final ArrayList<WNXBean> beanList) {
        return Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                try {
                    WNXDBManager.getInstance().saveWNXOrgList(beanList);
                    subscriber.onNext("success");
                    subscriber.onCompleted();
                } catch (Exception e) {
                    e.printStackTrace();
                    subscriber.onError(e);
                }

            }
        });
    }
}





